Skip to content

Support ushort for the mgh/mgz reader #1415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2025
Merged

Conversation

bthyreau
Copy link
Contributor

Hi,

nibabel currently crashes when loading an mgh/mgz file of type unsigned short.
As an consequence, recent FreeSurfer 7.4/8 clinical pipelines (that uses nibabel) now crashes on uint16 input MRIs (of any format, e.g nifti1) with an unfriendly python traceback, because it can't read back the mgz it just converted its input to.

I noted a bit of conflicting opinion on whether ushort should be supported for the MGH/MGZ file format.

  • Nibabel's freesurfer explicitly (e.g test_mghformat.py:bad_dtype_mgh) forbids it for new object creation. And ANTs ITK-based tools refuse both to read and to write ushort mgz.
  • But on the other hand, Freesurfer's own "mri_info" fully handle it, as so do other tools. In particular "mri_convert" obviously too (though not for explicit cast, ie. the -odt option!) considering FreeSurfer itself may create such ushort mgz out of well-supported regular ushort nifti files - without user's easily actionable workaround.

Therefore, officially supported or not, i suggest there is little drawback in nibabel not crashing when loading such ushort mgz.
The attached one-liner do that.

Copy link

codecov bot commented May 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.34%. Comparing base (db4c859) to head (643f7a1).
Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1415   +/-   ##
=======================================
  Coverage   95.34%   95.34%           
=======================================
  Files         209      209           
  Lines       29777    29777           
  Branches     3357     3357           
=======================================
  Hits        28390    28390           
  Misses        948      948           
  Partials      439      439           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@effigies
Copy link
Member

Thanks! I agree, if FreeSurfer creates it, we should be able to read it.

Do you have a link to FreeSurfer source that we can cross-reference? If we add it in a comment, it will make it easier to look up in the future, if they add more dtypes (however unlikely that may be).

@effigies
Copy link
Member

Looks like:

I guess I would follow the load_mgh.m fields, since others don't seem to be used.

@bthyreau
Copy link
Contributor Author

Thanks for the quick reply and fixing my botched commit. Indeed, it seems that FreeSurfer introduced the USHORT into mgz only in 2021, in freesurfer/freesurfer@b82c3a8, which may explain why it is not consistently mentionned everywhere.
I agree with you that supporting only the 5 types is probably the best for now, as the other types defined in mri.h are rarely used, if at all.

Copy link
Member

@effigies effigies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a comment so we can find these links again easily. Can you remove the now-failing test, or select another unsupported datatype (like float16)?

@bthyreau
Copy link
Contributor Author

Thanks a lot. I updated the comment in the code.
To update the failure-case test, i picked float64 (double) that is popular with FSL Nifti, yet poorly supported by FreeSurfer in general and fully unsupported by its mgz format

@effigies effigies merged commit e0ae56a into nipy:master May 16, 2025
34 of 42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants